/**
 * 找到最近的有相同X或Y坐标的点
 *
 * @USER: Administrator
 * @DATE: 2024/9/30 下午7:54
 * @VERSION: 1.0.0
 */
public class LC1779 {
    public static void main(String[] args) {
        LC1779 lc1779 = new LC1779();
        System.out.println(lc1779.nearestValidPoint(3,4,new int[][]{{1,2},{3,1},{2,4},{2,3},{4,4}}));
    }

    public int nearestValidPoint(int x, int y, int[][] points) {
        int index=-1;
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < points.length; i++) {
            if (points[i][0] == x && points[i][1] == y) {
                return i;
            }else if (points[i][0] == x || points[i][1] == y) {
                if (Math.abs(points[i][0] - x) + Math.abs(points[i][1] - y)<min){
                    min = Math.abs(points[i][0] - x) + Math.abs(points[i][1] - y);
                    index = i;
                }
            }
        }

        return index;
    }

}
